// var createError = require('http-errors');
var express = require('express');
var path = require('path');
let cors = require('cors');
// var cookieParser = require('cookie-parser');
// var logger = require('morgan');

// 引入路由模块
var indexRouter = require('./routes/index');
var usersRouter = require('./routes/users');

var app = express();

// view engine setup
// app.set('views', path.join(__dirname, 'views'));
// app.set('view engine', 'ejs');

// app.use(logger('dev'));

// 接参中间件
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
// app.use(cookieParser());

// 资源静态化
app.use(express.static(path.join(__dirname, 'public')));

// cors技术解决跨域
app.use(cors())

// 使用路由
app.use('/', indexRouter);
app.use('/users', usersRouter);

// 404错误
app.use(function(req, res, next) {
  // next(createError(404));
  res.send({code:404,msg:'找不到'})
});

// 统一错误处理
app.use(function(err, req, res, next) {
  res.send({code:400,msg:err})
  // // set locals, only providing error in development
  // res.locals.message = err.message;
  // res.locals.error = req.app.get('env') === 'development' ? err : {};

  // // render the error page
  // res.status(err.status || 500);
  // res.render('error');
});

module.exports = app;
